package com.example.music08.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.example.music08.R;
import com.example.music08.adapter.MusicAdapter;
import com.example.music08.entity.Music;
import com.example.music08.utils.MusicUtils;
import com.example.music08.utils.SqlUtils;

import java.util.ArrayList;
import java.util.List;

public class BlankFragment3 extends Fragment {
    List<Music.DataBean> list = new ArrayList<>();
    MusicAdapter musicAdapter;
    private ListView lvSc;

    public BlankFragment3() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_blank3, container, false);
        initView(inflate);
        musicAdapter = new MusicAdapter(list);
        lvSc.setAdapter(musicAdapter);

        cha();

        //长按弹出dialog,实现删除操作
        lvSc.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

                new AlertDialog.Builder(getActivity())
                        .setTitle("提示")
                        .setMessage("确定要删除吗")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                              //先删除后台数据   在删除页面数据
                              SqlUtils.getInstance(getActivity()).delete("sc",list.get(position).getId()+"");

                              list.remove(position);
                              musicAdapter.notifyDataSetChanged();
                                Toast.makeText(getActivity(), "删除成功", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        })
                        .show();


                return true;
            }
        });

        return inflate;
    }

    private void cha() {
        list.clear();
        List<Music.DataBean> beans = SqlUtils.getInstance(getActivity()).query("sc", "", "");
        list.addAll(beans);
        musicAdapter.notifyDataSetChanged();
        MusicUtils.getInstance().setList(beans);
    }

    private void initView(View inflate) {
        lvSc = (ListView) inflate.findViewById(R.id.lv_sc);
    }

    @Override
    public void onResume() {
        super.onResume();
        cha();
    }
}